﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using Psm.Logging;
using Psm.Container;

namespace Psm.Configuration
{
    public class PsmConfiguration : ConfigurationSection
    {
        private static ILogger logger = Repository.GetInstance<ILogger>();

        public static PsmConfiguration GetConfiguration()
        {
            var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            PsmConfiguration psmConfiguration = (PsmConfiguration)config.GetSection("Psm/Hosts");
            return psmConfiguration;
        }

       [ConfigurationProperty("", IsDefaultCollection=true, IsKey=false, IsRequired=true)]
        public HostConfigurationCollection Hosts
        {
            get
            {
                return base[""] as HostConfigurationCollection;
            }
 
            set
            {
                base[""] = value;
            }
        }
    }


    public class HostConfigurationCollection : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new HostConfiguration();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((HostConfiguration)element).Name;
        }

        protected override string ElementName
        {
            get
            {
                return "Host";
            }
        }

        protected override bool IsElementName(string elementName)
        {
            return !String.IsNullOrEmpty(elementName) && elementName == "Host";
        }

        public override ConfigurationElementCollectionType CollectionType
        {
            get
            {
                return ConfigurationElementCollectionType.BasicMap;
            }
        }

        public HostConfiguration this[int index]
        {
            get
            {
                return base.BaseGet(index) as HostConfiguration;
            }
        }
   
    }

    public class HostConfiguration : ConfigurationElement
    {
        public HostConfiguration()
        {
        }

        [ConfigurationProperty("name", IsRequired = true, IsKey = true)]
        public string Name
        {
            get { return base["name"] as string; }
            set { base["name"] = value; }
        }

        [ConfigurationProperty("Transport", IsRequired = true, IsKey = false)]
        public TransportConfiguration Transport
        {
            get { return base["Transport"] as TransportConfiguration; }
            set { base["Transport"] = value; }
        }

        [ConfigurationProperty("Protocol", IsRequired = true, IsKey = false)]
        public ProtocolConfiguration Protocol
        {
            get { return base["Protocol"] as ProtocolConfiguration; }
            set { base["Protocol"] = value; }
        }

        [ConfigurationProperty("Messaging", IsRequired = true, IsKey = false)]
        public MessagingConfiguration Messaging
        {
            get { return base["Messaging"] as MessagingConfiguration; }
            set { base["Messaging"] = value; }
        }

        [ConfigurationProperty("Processor", IsRequired = true, IsKey = false)]
        public ProcessorConfiguration Processor
        {
            get { return base["Processor"] as ProcessorConfiguration; }
            set { base["Processor"] = value; }
        }
    }


    public class TransportConfiguration : ConfigurationElement
    {
        public TransportConfiguration()
        {
        }

        [ConfigurationProperty("type", IsRequired = true)]
        public string Type
        {
            get { return base["type"] as string; }
            set { base["type"] = value; }
        }

        [ConfigurationProperty("composition", IsRequired = false)]
        public string Composition
        {
            get { return base["composition"] as string; }
            set { base["composition"] = value; }
        }
    }


    public class ProtocolConfiguration : ConfigurationElement
    {
        public ProtocolConfiguration()
        {
        }

          [ConfigurationProperty("type", IsRequired = true)]
        public string Type
        {
            get { return base["type"] as string; }
            set { base["type"] = value; }
        }

        [ConfigurationProperty("composition", IsRequired = false)]
        public string Composition
        {
            get { return base["composition"] as string; }
            set { base["composition"] = value; }
        }
    }


    public class MessagingConfiguration : ConfigurationElement
    {
        public MessagingConfiguration()
        {
        }

          [ConfigurationProperty("type", IsRequired = true)]
        public string Type
        {
            get { return base["type"] as string; }
            set { base["type"] = value; }
        }

        [ConfigurationProperty("composition", IsRequired = false)]
        public string Composition
        {
            get { return base["composition"] as string; }
            set { base["composition"] = value; }
        }
    }


    public class ProcessorConfiguration : ConfigurationElement
    {
        public ProcessorConfiguration()
        {
        }

         [ConfigurationProperty("type", IsRequired = true)]
        public string Type
        {
            get { return base["type"] as string; }
            set { base["type"] = value; }
        }

        [ConfigurationProperty("composition", IsRequired = false)]
        public string Composition
        {
            get { return base["composition"] as string; }
            set { base["composition"] = value; }
        }
    }
}
